home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / u_man / cat1 / perlre.z / perlre
Encoding:
Text File  |  2002-10-03  |  32.4 KB  |  793 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perlre - Perl regular expressions
  10.  
  11. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.      This page describes the syntax of regular expressions in Perl.  For a
  13.      description of how to _u_s_e regular expressions in matching operations,
  14.      plus various examples of the same, see m// and s/// in the _p_e_r_l_o_p
  15.      manpage.
  16.  
  17.      The matching operations can have various modifiers.  The modifiers that
  18.      relate to the interpretation of the regular expression inside are listed
  19.      below.  For the modifiers that alter the behaviour of the operation, see
  20.      the section on _m// in the _p_e_r_l_o_p manpage and the section on _s// in the
  21.      _p_e_r_l_o_p manpage.
  22.  
  23.      i   Do case-insensitive pattern matching.
  24.  
  25.          If use locale is in effect, the case map is taken from the current
  26.          locale.  See the _p_e_r_l_l_o_c_a_l_e manpage.
  27.  
  28.      m   Treat string as multiple lines.  That is, change "^" and "$" from
  29.          matching at only the very start or end of the string to the start or
  30.          end of any line anywhere within the string,
  31.  
  32.      s   Treat string as single line.  That is, change "." to match any
  33.          character whatsoever, even a newline, which it normally would not
  34.          match.
  35.  
  36.          The /s and /m modifiers both override the $* setting.  That is, no
  37.          matter what $* contains, /s without /m will force "^" to match only
  38.          at the beginning of the string and "$" to match only at the end (or
  39.          just before a newline at the end) of the string.  Together, as /ms,
  40.          they let the "." match any character whatsoever, while yet allowing
  41.          "^" and "$" to match, respectively, just after and just before
  42.          newlines within the string.
  43.  
  44.      x   Extend your pattern's legibility by permitting whitespace and
  45.          comments.
  46.  
  47.      These are usually written as "the /x modifier", even though the delimiter
  48.      in question might not actually be a slash.  In fact, any of these
  49.      modifiers may also be embedded within the regular expression itself using
  50.      the new (?...) construct.  See below.
  51.  
  52.      The /x modifier itself needs a little more explanation.  It tells the
  53.      regular expression parser to ignore whitespace that is neither
  54.      backslashed nor within a character class.  You can use this to break up
  55.      your regular expression into (slightly) more readable parts.  The #
  56.      character is also treated as a metacharacter introducing a comment, just
  57.      as in ordinary Perl code.  This also means that if you want real
  58.      whitespace or # characters in the pattern (outside of a character class,
  59.      where they are unaffected by /x), that you'll either have to escape them
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  71.  
  72.  
  73.  
  74.      or encode them using octal or hex escapes.  Taken together, these
  75.      features go a long way towards making Perl's regular expressions more
  76.      readable.  See the C-comment deletion code in the _p_e_r_l_o_p manpage.
  77.  
  78.      RRRReeeegggguuuullllaaaarrrr EEEExxxxpppprrrreeeessssssssiiiioooonnnnssss
  79.  
  80.      The patterns used in pattern matching are regular expressions such as
  81.      those supplied in the Version 8 regex routines.  (In fact, the routines
  82.      are derived (distantly) from Henry Spencer's freely redistributable
  83.      reimplementation of the V8 routines.)  See the section on _V_e_r_s_i_o_n _8
  84.      _R_e_g_u_l_a_r _E_x_p_r_e_s_s_i_o_n_s for details.
  85.  
  86.      In particular the following metacharacters have their standard _e_g_r_e_p-ish
  87.      meanings:
  88.  
  89.          \   Quote the next metacharacter
  90.          ^   Match the beginning of the line
  91.          .   Match any character (except newline)
  92.          $   Match the end of the line (or before newline at the end)
  93.          |   Alternation
  94.          ()  Grouping
  95.          []  Character class
  96.  
  97.      By default, the "^" character is guaranteed to match at only the
  98.      beginning of the string, the "$" character at only the end (or before the
  99.      newline at the end) and Perl does certain optimizations with the
  100.      assumption that the string contains only one line.  Embedded newlines
  101.      will not be matched by "^" or "$".  You may, however, wish to treat a
  102.      string as a multi-line buffer, such that the "^" will match after any
  103.      newline within the string, and "$" will match before any newline.  At the
  104.      cost of a little more overhead, you can do this by using the /m modifier
  105.      on the pattern match operator.  (Older programs did this by setting $*,
  106.      but this practice is now deprecated.)
  107.  
  108.      To facilitate multi-line substitutions, the "." character never matches a
  109.      newline unless you use the /s modifier, which in effect tells Perl to
  110.      pretend the string is a single line--even if it isn't.  The /s modifier
  111.      also overrides the setting of $*, in case you have some (badly behaved)
  112.      older code that sets it in another module.
  113.  
  114.      The following standard quantifiers are recognized:
  115.  
  116.          *      Match 0 or more times
  117.          +      Match 1 or more times
  118.          ?      Match 1 or 0 times
  119.          {n}    Match exactly n times
  120.          {n,}   Match at least n times
  121.          {n,m}  Match at least n but not more than m times
  122.  
  123.      (If a curly bracket occurs in any other context, it is treated as a
  124.      regular character.)  The "*" modifier is equivalent to {0,}, the "+"
  125.      modifier to {1,}, and the "?" modifier to {0,1}.  n and m are limited to
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  137.  
  138.  
  139.  
  140.      integral values less than 65536.
  141.  
  142.      By default, a quantified subpattern is "greedy", that is, it will match
  143.      as many times as possible (given a particular starting location) while
  144.      still allowing the rest of the pattern to match.  If you want it to match
  145.      the minimum number of times possible, follow the quantifier with a "?".
  146.      Note that the meanings don't change, just the "greediness":
  147.  
  148.          *?     Match 0 or more times
  149.          +?     Match 1 or more times
  150.          ??     Match 0 or 1 time
  151.          {n}?   Match exactly n times
  152.          {n,}?  Match at least n times
  153.          {n,m}? Match at least n but not more than m times
  154.  
  155.      Because patterns are processed as double quoted strings, the following
  156.      also work:
  157.  
  158.          \t          tab                   (HT, TAB)
  159.          \n          newline               (LF, NL)
  160.          \r          return                (CR)
  161.          \f          form feed             (FF)
  162.          \a          alarm (bell)          (BEL)
  163.          \e          escape (think troff)  (ESC)
  164.          \033        octal char (think of a PDP-11)
  165.          \x1B        hex char
  166.          \c[         control char
  167.          \l          lowercase next char (think vi)
  168.          \u          uppercase next char (think vi)
  169.          \L          lowercase till \E (think vi)
  170.          \U          uppercase till \E (think vi)
  171.          \E          end case modification (think vi)
  172.          \Q          quote (disable) pattern metacharacters till \E
  173.  
  174.      If use locale is in effect, the case map used by \l, \L, \u and \U is
  175.      taken from the current locale.  See the _p_e_r_l_l_o_c_a_l_e manpage.
  176.  
  177.      You cannot include a literal $ or @ within a \Q sequence.  An unescaped $
  178.      or @ interpolates the corresponding variable, while escaping will cause
  179.      the literal string \$ to be matched.  You'll need to write something like
  180.      m/\Quser\E\@\Qhost/.
  181.  
  182.      In addition, Perl defines the following:
  183.  
  184.          \w  Match a "word" character (alphanumeric plus "_")
  185.          \W  Match a non-word character
  186.          \s  Match a whitespace character
  187.          \S  Match a non-whitespace character
  188.          \d  Match a digit character
  189.          \D  Match a non-digit character
  190.  
  191.      A \w matches a single alphanumeric character, not a whole word.  To match
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  203.  
  204.  
  205.  
  206.      a word you'd need to say \w+.  If use locale is in effect, the list of
  207.      alphabetic characters generated by \w is taken from the current locale.
  208.      See the _p_e_r_l_l_o_c_a_l_e manpage. You may use \w, \W, \s, \S, \d, and \D within
  209.      character classes (though not as either end of a range).
  210.  
  211.      Perl defines the following zero-width assertions:
  212.  
  213.          \b  Match a word boundary
  214.          \B  Match a non-(word boundary)
  215.          \A  Match at only beginning of string
  216.          \Z  Match at only end of string (or before newline at the end)
  217.          \G  Match only where previous m//g left off (works only with /g)
  218.  
  219.      A word boundary (\b) is defined as a spot between two characters that has
  220.      a \w on one side of it and a \W on the other side of it (in either
  221.      order), counting the imaginary characters off the beginning and end of
  222.      the string as matching a \W.  (Within character classes \b represents
  223.      backspace rather than a word boundary.)  The \A and \Z are just like "^"
  224.      and "$", except that they won't match multiple times when the /m modifier
  225.      is used, while "^" and "$" will match at every internal line boundary.
  226.      To match the actual end of the string, not ignoring newline, you can use
  227.      \Z(?!\n).  The \G assertion can be used to chain global matches (using
  228.      m//g), as described in the section on _R_e_g_e_x_p _Q_u_o_t_e-_L_i_k_e _O_p_e_r_a_t_o_r_s in the
  229.      _p_e_r_l_o_p manpage.
  230.  
  231.      It is also useful when writing lex-like scanners, when you have several
  232.      patterns that you want to match against consequent substrings of your
  233.      string, see the previous reference.  The actual location where \G will
  234.      match can also be influenced by using pos() as an lvalue.  See the pos
  235.      entry in the _p_e_r_l_f_u_n_c manpage.
  236.  
  237.      When the bracketing construct ( ... ) is used, \<digit> matches the
  238.      digit'th substring.  Outside of the pattern, always use "$" instead of
  239.      "\" in front of the digit.  (While the \<digit> notation can on rare
  240.      occasion work outside the current pattern, this should not be relied
  241.      upon.  See the WARNING below.) The scope of $<digit> (and $`, $&, and $')
  242.      extends to the end of the enclosing BLOCK or eval string, or to the next
  243.      successful pattern match, whichever comes first.  If you want to use
  244.      parentheses to delimit a subpattern (e.g., a set of alternatives) without
  245.      saving it as a subpattern, follow the ( with a ?:.
  246.  
  247.      You may have as many parentheses as you wish.  If you have more than 9
  248.      substrings, the variables $10, $11, ... refer to the corresponding
  249.      substring.  Within the pattern, \10, \11, etc. refer back to substrings
  250.      if there have been at least that many left parentheses before the
  251.      backreference.  Otherwise (for backward compatibility) \10 is the same as
  252.      \010, a backspace, and \11 the same as \011, a tab.  And so on.  (\1
  253.      through \9 are always backreferences.)
  254.  
  255.      $+ returns whatever the last bracket match matched.  $& returns the
  256.      entire matched string.  ($0 used to return the same thing, but not any
  257.      more.)  $` returns everything before the matched string.  $' returns
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  269.  
  270.  
  271.  
  272.      everything after the matched string.  Examples:
  273.  
  274.          s/^([^ ]*) *([^ ]*)/$2 $1/;     # swap first two words
  275.  
  276.          if (/Time: (..):(..):(..)/) {
  277.              $hours = $1;
  278.              $minutes = $2;
  279.              $seconds = $3;
  280.          }
  281.  
  282.      Once perl sees that you need one of $&, $` or $' anywhere in the program,
  283.      it has to provide them on each and every pattern match.  This can slow
  284.      your program down.  The same mechanism that handles these provides for
  285.      the use of $1, $2, etc., so you pay the same price for each pattern that
  286.      contains capturing parentheses. But if you never use $&, etc., in your
  287.      script, then patterns _w_i_t_h_o_u_t capturing parentheses won't be penalized.
  288.      So avoid $&, $', and $` if you can, but if you can't (and some algorithms
  289.      really appreciate them), once you've used them once, use them at will,
  290.      because you've already paid the price.  As of 5.005, $& is not so costly
  291.      as the other two.
  292.  
  293.      Backslashed metacharacters in Perl are alphanumeric, such as \b, \w, \n.
  294.      Unlike some other regular expression languages, there are no backslashed
  295.      symbols that aren't alphanumeric.  So anything that looks like \\, \(,
  296.      \), \<, \>, \{, or \} is always interpreted as a literal character, not a
  297.      metacharacter.  This was once used in a common idiom to disable or quote
  298.      the special meanings of regular expression metacharacters in a string
  299.      that you want to use for a pattern. Simply quote all non-alphanumeric
  300.      characters:
  301.  
  302.          $pattern =~ s/(\W)/\\$1/g;
  303.  
  304.      Now it is much more common to see either the _q_u_o_t_e_m_e_t_a() function or the
  305.      \Q escape sequence used to disable all metacharacters' special meanings
  306.      like this:
  307.  
  308.          /$unquoted\Q$quoted\E$unquoted/
  309.  
  310.      Perl defines a consistent extension syntax for regular expressions.  The
  311.      syntax is a pair of parentheses with a question mark as the first thing
  312.      within the parentheses (this was a syntax error in older versions of
  313.      Perl).  The character after the question mark gives the function of the
  314.      extension.  Several extensions are already supported:
  315.  
  316.      (?#text)  A comment.  The text is ignored.  If the /x switch is used to
  317.                enable whitespace formatting, a simple # will suffice.
  318.  
  319.      (?:pattern)
  320.                This is for clustering, not capturing; it groups subexpressions
  321.                like "()", but doesn't make backreferences as "()" does.  So
  322.  
  323.                    @fields = split(/\b(?:a|b|c)\b/)
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  335.  
  336.  
  337.  
  338.                is like
  339.  
  340.                    @fields = split(/\b(a|b|c)\b/)
  341.  
  342.                but doesn't spit out extra fields.
  343.  
  344.      (?=pattern)
  345.                A zero-width positive lookahead assertion.  For example,
  346.                /\w+(?=\t)/ matches a word followed by a tab, without including
  347.                the tab in $&.
  348.  
  349.      (?!pattern)
  350.                A zero-width negative lookahead assertion.  For example
  351.                /foo(?!bar)/ matches any occurrence of "foo" that isn't
  352.                followed by "bar".  Note however that lookahead and lookbehind
  353.                are NOT the same thing.  You cannot use this for lookbehind.
  354.  
  355.                If you are looking for a "bar" that isn't preceded by a "foo",
  356.                /(?!foo)bar/ will not do what you want.  That's because the
  357.                (?!foo) is just saying that the next thing cannot be "foo"--and
  358.                it's not, it's a "bar", so "foobar" will match.  You would have
  359.                to do something like /(?!foo)...bar/ for that.   We say "like"
  360.                because there's the case of your "bar" not having three
  361.                characters before it.  You could cover that this way:
  362.                /(?:(?!foo)...|^.{0,2})bar/.  Sometimes it's still easier just
  363.                to say:
  364.  
  365.                    if (/bar/ && $` !~ /foo$/)
  366.  
  367.  
  368.      (?imstx)  One or more embedded pattern-match modifiers.  This is
  369.                particularly useful for patterns that are specified in a table
  370.                somewhere, some of which want to be case sensitive, and some of
  371.                which don't.  The case insensitive ones need to include merely
  372.                (?i) at the front of the pattern.  For example:
  373.  
  374.                    $pattern = "foobar";
  375.                    if ( /$pattern/i )
  376.  
  377.                    # more flexible:
  378.  
  379.                    $pattern = "(?i)foobar";
  380.                    if ( /$pattern/ )
  381.  
  382.  
  383.      A question mark was chosen for this and for the new minimal-matching
  384.      construct because 1) question mark is pretty rare in older regular
  385.      expressions, and 2) whenever you see one, you should stop and "question"
  386.      exactly what is going on.  That's psychology...
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  401.  
  402.  
  403.  
  404.      BBBBaaaacccckkkkttttrrrraaaacccckkkkiiiinnnngggg
  405.  
  406.      A fundamental feature of regular expression matching involves the notion
  407.      called _b_a_c_k_t_r_a_c_k_i_n_g, which is currently used (when needed) by all regular
  408.      expression quantifiers, namely *, *?, +, +?, {n,m}, and {n,m}?.
  409.  
  410.      For a regular expression to match, the _e_n_t_i_r_e regular expression must
  411.      match, not just part of it.  So if the beginning of a pattern containing
  412.      a quantifier succeeds in a way that causes later parts in the pattern to
  413.      fail, the matching engine backs up and recalculates the beginning part--
  414.      that's why it's called backtracking.
  415.  
  416.      Here is an example of backtracking:  Let's say you want to find the word
  417.      following "foo" in the string "Food is on the foo table.":
  418.  
  419.          $_ = "Food is on the foo table.";
  420.          if ( /\b(foo)\s+(\w+)/i ) {
  421.              print "$2 follows $1.\n";
  422.          }
  423.  
  424.      When the match runs, the first part of the regular expression (\b(foo))
  425.      finds a possible match right at the beginning of the string, and loads up
  426.      $1 with "Foo".  However, as soon as the matching engine sees that there's
  427.      no whitespace following the "Foo" that it had saved in $1, it realizes
  428.      its mistake and starts over again one character after where it had the
  429.      tentative match.  This time it goes all the way until the next occurrence
  430.      of "foo". The complete regular expression matches this time, and you get
  431.      the expected output of "table follows foo."
  432.  
  433.      Sometimes minimal matching can help a lot.  Imagine you'd like to match
  434.      everything between "foo" and "bar".  Initially, you write something like
  435.      this:
  436.  
  437.          $_ =  "The food is under the bar in the barn.";
  438.          if ( /foo(.*)bar/ ) {
  439.              print "got <$1>\n";
  440.          }
  441.  
  442.      Which perhaps unexpectedly yields:
  443.  
  444.        got <d is under the bar in the >
  445.  
  446.      That's because .* was greedy, so you get everything between the _f_i_r_s_t
  447.      "foo" and the _l_a_s_t "bar".  In this case, it's more effective to use
  448.      minimal matching to make sure you get the text between a "foo" and the
  449.      first "bar" thereafter.
  450.  
  451.          if ( /foo(.*?)bar/ ) { print "got <$1>\n" }
  452.        got <d is under the >
  453.  
  454.      Here's another example: let's say you'd like to match a number at the end
  455.      of a string, and you also want to keep the preceding part the match.  So
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  467.  
  468.  
  469.  
  470.      you write this:
  471.  
  472.          $_ = "I have 2 numbers: 53147";
  473.          if ( /(.*)(\d*)/ ) {                                # Wrong!
  474.              print "Beginning is <$1>, number is <$2>.\n";
  475.          }
  476.  
  477.      That won't work at all, because .* was greedy and gobbled up the whole
  478.      string. As \d* can match on an empty string the complete regular
  479.      expression matched successfully.
  480.  
  481.          Beginning is <I have 2 numbers: 53147>, number is <>.
  482.  
  483.      Here are some variants, most of which don't work:
  484.  
  485.          $_ = "I have 2 numbers: 53147";
  486.          @pats = qw{
  487.              (.*)(\d*)
  488.              (.*)(\d+)
  489.              (.*?)(\d*)
  490.              (.*?)(\d+)
  491.              (.*)(\d+)$
  492.              (.*?)(\d+)$
  493.              (.*)\b(\d+)$
  494.              (.*\D)(\d+)$
  495.          };
  496.  
  497.          for $pat (@pats) {
  498.              printf "%-12s ", $pat;
  499.              if ( /$pat/ ) {
  500.                  print "<$1> <$2>\n";
  501.              } else {
  502.                  print "FAIL\n";
  503.              }
  504.          }
  505.  
  506.      That will print out:
  507.  
  508.          (.*)(\d*)    <I have 2 numbers: 53147> <>
  509.          (.*)(\d+)    <I have 2 numbers: 5314> <7>
  510.          (.*?)(\d*)   <> <>
  511.          (.*?)(\d+)   <I have > <2>
  512.          (.*)(\d+)$   <I have 2 numbers: 5314> <7>
  513.          (.*?)(\d+)$  <I have 2 numbers: > <53147>
  514.          (.*)\b(\d+)$ <I have 2 numbers: > <53147>
  515.          (.*\D)(\d+)$ <I have 2 numbers: > <53147>
  516.  
  517.      As you see, this can be a bit tricky.  It's important to realize that a
  518.      regular expression is merely a set of assertions that gives a definition
  519.      of success.  There may be 0, 1, or several different ways that the
  520.      definition might succeed against a particular string.  And if there are
  521.      multiple ways it might succeed, you need to understand backtracking to
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  533.  
  534.  
  535.  
  536.      know which variety of success you will achieve.
  537.  
  538.      When using lookahead assertions and negations, this can all get even
  539.      tricker.  Imagine you'd like to find a sequence of non-digits not
  540.      followed by "123".  You might try to write that as
  541.  
  542.              $_ = "ABC123";
  543.              if ( /^\D*(?!123)/ ) {                          # Wrong!
  544.                  print "Yup, no 123 in $_\n";
  545.              }
  546.  
  547.      But that isn't going to match; at least, not the way you're hoping.  It
  548.      claims that there is no 123 in the string.  Here's a clearer picture of
  549.      why it that pattern matches, contrary to popular expectations:
  550.  
  551.          $x = 'ABC123' ;
  552.          $y = 'ABC445' ;
  553.  
  554.          print "1: got $1\n" if $x =~ /^(ABC)(?!123)/ ;
  555.          print "2: got $1\n" if $y =~ /^(ABC)(?!123)/ ;
  556.  
  557.          print "3: got $1\n" if $x =~ /^(\D*)(?!123)/ ;
  558.          print "4: got $1\n" if $y =~ /^(\D*)(?!123)/ ;
  559.  
  560.      This prints
  561.  
  562.          2: got ABC
  563.          3: got AB
  564.          4: got ABC
  565.  
  566.      You might have expected test 3 to fail because it seems to a more general
  567.      purpose version of test 1.  The important difference between them is that
  568.      test 3 contains a quantifier (\D*) and so can use backtracking, whereas
  569.      test 1 will not.  What's happening is that you've asked "Is it true that
  570.      at the start of $x, following 0 or more non-digits, you have something
  571.      that's not 123?"  If the pattern matcher had let \D* expand to "ABC",
  572.      this would have caused the whole pattern to fail.  The search engine will
  573.      initially match \D* with "ABC".  Then it will try to match (?!123 with
  574.      "123", which of course fails.  But because a quantifier (\D*) has been
  575.      used in the regular expression, the search engine can backtrack and retry
  576.      the match differently in the hope of matching the complete regular
  577.      expression.
  578.  
  579.      The pattern really, _r_e_a_l_l_y wants to succeed, so it uses the standard
  580.      pattern back-off-and-retry and lets \D* expand to just "AB" this time.
  581.      Now there's indeed something following "AB" that is not "123".  It's in
  582.      fact "C123", which suffices.
  583.  
  584.      We can deal with this by using both an assertion and a negation.  We'll
  585.      say that the first part in $1 must be followed by a digit, and in fact,
  586.      it must also be followed by something that's not "123".  Remember that
  587.      the lookaheads are zero-width expressions--they only look, but don't
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  599.  
  600.  
  601.  
  602.      consume any of the string in their match.  So rewriting this way produces
  603.      what you'd expect; that is, case 5 will fail, but case 6 succeeds:
  604.  
  605.          print "5: got $1\n" if $x =~ /^(\D*)(?=\d)(?!123)/ ;
  606.          print "6: got $1\n" if $y =~ /^(\D*)(?=\d)(?!123)/ ;
  607.  
  608.          6: got ABC
  609.  
  610.      In other words, the two zero-width assertions next to each other work as
  611.      though they're ANDed together, just as you'd use any builtin assertions:
  612.      /^$/ matches only if you're at the beginning of the line AND the end of
  613.      the line simultaneously.  The deeper underlying truth is that
  614.      juxtaposition in regular expressions always means AND, except when you
  615.      write an explicit OR using the vertical bar.  /ab/ means match "a" AND
  616.      (then) match "b", although the attempted matches are made at different
  617.      positions because "a" is not a zero-width assertion, but a one-width
  618.      assertion.
  619.  
  620.      One warning: particularly complicated regular expressions can take
  621.      exponential time to solve due to the immense number of possible ways they
  622.      can use backtracking to try match.  For example this will take a very
  623.      long time to run
  624.  
  625.          /((a{0,5}){0,5}){0,5}/
  626.  
  627.      And if you used *'s instead of limiting it to 0 through 5 matches, then
  628.      it would take literally forever--or until you ran out of stack space.
  629.  
  630.      VVVVeeeerrrrssssiiiioooonnnn 8888 RRRReeeegggguuuullllaaaarrrr EEEExxxxpppprrrreeeessssssssiiiioooonnnnssss
  631.  
  632.      In case you're not familiar with the "regular" Version 8 regex routines,
  633.      here are the pattern-matching rules not described above.
  634.  
  635.      Any single character matches itself, unless it is a _m_e_t_a_c_h_a_r_a_c_t_e_r with a
  636.      special meaning described here or above.  You can cause characters that
  637.      normally function as metacharacters to be interpreted literally by
  638.      prefixing them with a "\" (e.g., "\." matches a ".", not any character;
  639.      "\\" matches a "\").  A series of characters matches that series of
  640.      characters in the target string, so the pattern blurfl would match
  641.      "blurfl" in the target string.
  642.  
  643.      You can specify a character class, by enclosing a list of characters in
  644.      [], which will match any one character from the list.  If the first
  645.      character after the "[" is "^", the class matches any character not in
  646.      the list.  Within a list, the "-" character is used to specify a range,
  647.      so that a-z represents all characters between "a" and "z", inclusive.  If
  648.      you want "-" itself to be a member of a class, put it at the start or end
  649.      of the list, or escape it with a backslash.  (The following all specify
  650.      the same class of three characters: [-az], [az-], and [a\-z].  All are
  651.      different from [a-z], which specifies a class containing twenty-six
  652.      characters.)
  653.  
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  665.  
  666.  
  667.  
  668.      Characters may be specified using a metacharacter syntax much like that
  669.      used in C: "\n" matches a newline, "\t" a tab, "\r" a carriage return,
  670.      "\f" a form feed, etc.  More generally, \_n_n_n, where _n_n_n is a string of
  671.      octal digits, matches the character whose ASCII value is _n_n_n.  Similarly,
  672.      \x_n_n, where _n_n are hexadecimal digits, matches the character whose ASCII
  673.      value is _n_n. The expression \c_x matches the ASCII character control-_x.
  674.      Finally, the "." metacharacter matches any character except "\n" (unless
  675.      you use /s).
  676.  
  677.      You can specify a series of alternatives for a pattern using "|" to
  678.      separate them, so that fee|fie|foe will match any of "fee", "fie", or
  679.      "foe" in the target string (as would f(e|i|o)e).  The first alternative
  680.      includes everything from the last pattern delimiter ("(", "[", or the
  681.      beginning of the pattern) up to the first "|", and the last alternative
  682.      contains everything from the last "|" to the next pattern delimiter.  For
  683.      this reason, it's common practice to include alternatives in parentheses,
  684.      to minimize confusion about where they start and end.
  685.  
  686.      Alternatives are tried from left to right, so the first alternative found
  687.      for which the entire expression matches, is the one that is chosen. This
  688.      means that alternatives are not necessarily greedy. For example: when
  689.      mathing foo|foot against "barefoot", only the "foo" part will match, as
  690.      that is the first alternative tried, and it successfully matches the
  691.      target string. (This might not seem important, but it is important when
  692.      you are capturing matched text using parentheses.)
  693.  
  694.      Also remember that "|" is interpreted as a literal within square
  695.      brackets, so if you write [fee|fie|foe] you're really only matching
  696.      [feio|].
  697.  
  698.      Within a pattern, you may designate subpatterns for later reference by
  699.      enclosing them in parentheses, and you may refer back to the _nth
  700.      subpattern later in the pattern using the metacharacter \_n.  Subpatterns
  701.      are numbered based on the left to right order of their opening
  702.      parenthesis.  A backreference matches whatever actually matched the
  703.      subpattern in the string being examined, not the rules for that
  704.      subpattern.  Therefore, (0|0x)\d*\s\1\d* will match "0x1234 0x4321", but
  705.      not "0x1234 01234", because subpattern 1 actually matched "0x", even
  706.      though the rule 0|0x could potentially match the leading 0 in the second
  707.      number.
  708.  
  709.      WWWWAAAARRRRNNNNIIIINNNNGGGG oooonnnn \\\\1111 vvvvssss $$$$1111
  710.  
  711.      Some people get too used to writing things like:
  712.  
  713.          $pattern =~ s/(\W)/\\\1/g;
  714.  
  715.      This is grandfathered for the RHS of a substitute to avoid shocking the
  716.      sssseeeedddd addicts, but it's a dirty habit to get into.  That's because in
  717.      PerlThink, the righthand side of a s/// is a double-quoted string.  \1 in
  718.      the usual double-quoted string means a control-A.  The customary Unix
  719.      meaning of \1 is kludged in for s///.  However, if you get into the habit
  720.  
  721.  
  722.  
  723.                                                                        PPPPaaaaggggeeee 11111111
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. PPPPEEEERRRRLLLLRRRREEEE((((1111))))                                                            PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  731.  
  732.  
  733.  
  734.      of doing that, you get yourself into trouble if you then add an /e
  735.      modifier.
  736.  
  737.          s/(\d+)/ \1 + 1 /eg;        # causes warning under -w
  738.  
  739.      Or if you try to do
  740.  
  741.          s/(\d+)/\1000/;
  742.  
  743.      You can't disambiguate that by saying \{1}000, whereas you can fix it
  744.      with ${1}000.  Basically, the operation of interpolation should not be
  745.      confused with the operation of matching a backreference.  Certainly they
  746.      mean two different things on the _l_e_f_t side of the s///.
  747.  
  748.      SSSSEEEEEEEE AAAALLLLSSSSOOOO
  749.  
  750.      the section on _R_e_g_e_x_p _Q_u_o_t_e-_L_i_k_e _O_p_e_r_a_t_o_r_s in the _p_e_r_l_o_p manpage.
  751.  
  752.      the pos entry in the _p_e_r_l_f_u_n_c manpage.
  753.  
  754.      the _p_e_r_l_l_o_c_a_l_e manpage.
  755.  
  756.      _M_a_s_t_e_r_i_n_g _R_e_g_u_l_a_r _E_x_p_r_e_s_s_i_o_n_s (see the _p_e_r_l_b_o_o_k manpage) by Jeffrey
  757.      Friedl.
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.                                                                        PPPPaaaaggggeeee 11112222
  790.  
  791.  
  792.  
  793.